﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LimitOrderBookSim.Commons;

namespace LimitOrderBookSim.Assets
{
    public  class OrderPool
    {

         Writer _msg_log;
         public OrderBook BOOK { get; set; }
         Message[] _messages;

             Message[] Messages
            {
                get
                {
                    return _messages;
                }
                set
                {
                    _messages = value;
                        
                }
            }

         int size = 0;
         int current_max_size = SEQUENCE;
         const int SEQUENCE = 100000;
         object sync = new object();
         readonly object l = new object();

         public OrderPool()
        {
            Messages = new Message[SEQUENCE];
            _msg_log = new Writer("msg.txt");
            BOOK = new OrderBook(this);
        }

        public  void Resize()
        {
            current_max_size = size + SEQUENCE;
            Array.Resize(ref _messages, current_max_size);
        }

        public  void Add(Message message)
        {
            lock (sync)
            {
                if (size == current_max_size)
                {
                    Resize();
                }

                Messages[size] = message;
                message.Order.ID = size;
                #if LOG
                   _msg_log.Write(message.Order.ID + Environment.NewLine);
                #endif
                size++;

            }
            
            
            BOOK.Process(message);
        }


        public Order Get(int id)
        {
            return
                Messages[id].Order;
        }



    }
}
